﻿using System;
using System.Net;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine.StarShips;

namespace SCG.SolarVengeanceEngine
{
    //A list of StarShips with helper methods
    public class StarShipList : List<StarShip>
    {   
        //Can at least one of the ships execute an order?
        public bool CanExecuteOrder(OrderType ot)
        {
            foreach (StarShip ship in this)
                if (ship.CanExecuteOrder(ot))
                    return true;
            return false;
        }
        public bool AllCanExecuteOrder(OrderType ot)
        {
            foreach (StarShip ship in this)
                if (!ship.CanExecuteOrder(ot))
                    return false;
            return true;
        }

        //Can at least one of the ships target the specified StarSystem?
        public bool CanTargetStarSystem(StarSystem system)
        {
            foreach (StarShip ship in this)
                if (ship.CanTargetStarSystem(system, false))
                    return true;
            return false;
        }

        //can at least one of the ships target a specific starship?
        public bool CanTargetStarShip(StarShip target, bool respectAlliance)
        {
            foreach (StarShip ship in this)
                if (ship.CanTargetStarShip(target, respectAlliance))
                    return true;
            return false;
        }

        //return true if at least one ship has an order
        public bool HasOrders
        {
            get
            {
                foreach (StarShip ship in this)
                    if (ship.Order != null)
                        return true;
                return false;
            }
        }

        //do all the ships share the same order?
        public bool HaveSameOrders
        {
            get
            {
                if (Count == 0)
                    return false;
                string os = this[0].Order == null ? "Null" : this[0].Order.ToString();
                for (int i = 1; i < Count; i++)
                {
                    string os2 = this[i].Order == null ? "Null" : this[i].Order.ToString();
                    if (os != os2)
                        return false;
                }
                return true;
            }
        }

        //do any of the ships have auto-attack on?
        public bool AutoAttack
        {
            get
            {
                foreach (StarShip ship in this)
                    if (ship.AttackAdjacent)
                        return true;
                return false;
            }
        }

        //get the first StarShip in the list that can build other StarShips
        public ResourceStarShip Builder
        {
            get
            {
                foreach (StarShip ship in this)
                    if (ship.CanExecuteOrder(OrderType.ShipBuildStarShip))
                        if (ship is ResourceStarShip)
                            return ship as ResourceStarShip;
                return null;
            }
        }
    }
}
